/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.verolux.soft.debaraz.service;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.Sellout;
import com.verolux.soft.debaraz.dao.SelloutDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author ut1e8p
 */
@Service(value="selloutService")
public class SelloutServiceImpl implements SelloutService {

    @Autowired
    private SelloutDAO selloutDAO;

    /**
     *
     * @param sellout
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ApplicationException.class)
    public void create(Sellout sellout) throws ApplicationException {
       try {
            if (sellout.getId() != null) {
                sellout.setId(null);
            }
            selloutDAO.save(sellout);
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     * 
     * @param userId
     * @return
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly=true)
    public Sellout findByUserId(String userId) throws ApplicationException {
        try {
            Sellout sellout = selloutDAO.findByUserId(userId);
            return sellout;
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     *
     * @param userId
     * @return
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly=true)
    public Sellout findActiveByUserId(String userId) throws ApplicationException {
        try {
            Sellout sellout = selloutDAO.findActiveByUserId(userId);
            return sellout;
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }
}
